﻿
@{
    ViewBag.Title = "PortalTemplateEdit";
    Layout = "~/Views/Shared/_LayoutBlank.cshtml";
}


<script>

    var _mode = "create";//modify
    var _validator;
    var _data = null;

    $(document).ready(function () {
        $("[keyenter]").keypress(function (e) {
            if (e.keyCode == 13) {
                save();
            }
        });

        _validator = $("#form").validate({
            onfocusout: false,
            onkeyup: false,
            showErrors: showValidationErrors,
            rules: {
                "txtName": "required",
                "txtPreviewImageUrl": "required",
                "txtBackgroundImageUrl": "required",
                "txtTemplate": "required",
                "txtPreviewHtml": "required"
            },
            messages: {
                "txtName": "请输入名称；",
                "txtPreviewImageUrl": "请输入效果图；",
                "txtBackgroundImageUrl": "请输入默认背景图；",
                "txtTemplate": "请输入模版内容；",
                "txtPreviewHtml": "请输入模版预览Html"
            }
        });

        load();
    });

    function load() {
        var id = getQueryString("id");
        if (id == undefined || id == "") {
            return;
        }

        _mode = "modify";
       // $("#btnRemove").show();

        var loadLayerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        $.ajax({
            url: "/Api/PortalPresetTemplate/GetTemplate?id=" + id,
            type: "POST",
            dataType: "json",
            success: function (data, status, jqXHR) {
                layer.close(loadLayerIndex);
                if (data.Success) {
                    _data = data.Data;
                    $("#txtName").val(_data.Name);
                    $("#txtPreviewImageUrl").val(_data.PreviewImageUrl);
                    $("#txtBackgroundImageUrl").val(_data.BackgroundImageUrl);
                    $("#txtTemplate").val(_data.Template);
                    $("#txtPreviewHtml").val(_data.PreviewHtml);
                    $("#txtDescription").val(_data.Description);
                } else {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest) {
                layer.close(loadLayerIndex);
                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function save() {
        if (_validator.form() == false) {
            return;
        }

        var loadLayerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        var url = "/Api/PortalPresetTemplate/CreateTemplate";

        if (_data == undefined || _data == null) {
            _data = new Object();
        } else {
            url = "/Api/PortalPresetTemplate/UpdateTemplate";
        }

        _data.Name = $("#txtName").val();
        _data.PreviewImageUrl = $("#txtPreviewImageUrl").val();
        _data.BackgroundImageUrl = $("#txtBackgroundImageUrl").val();
        _data.Template = $("#txtTemplate").val();
        _data.PreviewHtml = $("#txtPreviewHtml").val();
        _data.Description = $("#txtDescription").val();

        $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            data: JSON.stringify(_data),
            success: function (data, status, jqXHR) {
                layer.close(loadLayerIndex);
                if (data.Success) {
                    var index = parent.layer.getFrameIndex(window.name);
                    if (_mode == "create") {
                        parent.loadDataAndCloseLayer(index);
                    } else {
                        parent.loadDataOnPageAndCloseLayer(index);
                    }
                } else {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest) {
                layer.close(loadLayerIndex);
                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function removeData() {

        layerAlert("禁止删除");
        return;

        if (_data == null || _data.Id == "" || _data.Id == "") {
            return;
        }

        var msg = "是否确认删除？"

        var confirmLayerIndex = layer.confirm(msg, {
            btn: ['确认', '取消'], //按钮
            shade: [0.4, '#393D49'],
            title: false,
            closeBtn: false,
            shift: _layerShift
        }, function () {
            layer.close(confirmLayerIndex);

            var loadLayerIndex = layer.load(0, {
                shade: [0.2, '#fff']
            });

            $.ajax({
                url: "/Api/PortalPresetTemplate/RemoveTemplate?id=" + _data.Id,
                type: "POST",
                dataType: "json",
                success: function (data, status, jqXHR) {
                    if (data.Success) {
                        //var index = parent.layer.getFrameIndex(window.name);
                        //parent.loadDataOnPageAndCloseLayer(index);
                        parent.window.location.href = "/Home/PortalTemplate";

                    } else {
                        layer.closeAll();
                        layerAlert(data.Message);
                    }
                },
                error: function (xmlHttpRequest) {
                    layer.closeAll();
                    alert("Error: " + xmlHttpRequest.status);
                }
            });
        });
    }

    function cancel() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }

</script>


<div style="margin-left:20px; margin-right:20px; margin-top:20px;">
    <span id="spanTitle" class="font_black_24">微主页模版</span>
</div>

<div style=" background-color:#ccc; margin-left:20px; margin-right:20px; margin-top:10px; height:1px;">

</div>

<div style=" position:absolute; overflow:auto ;margin-top:25px;left:30px; right:30px; bottom:60px; top:50px; ">

    <form id="form">
        <input type="hidden" id="txtId" />
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="130" height="36">名称：</td>
                <td><input id="txtName" name="txtName" type="text" class="input_16" style="width:96%; " maxlength="25" /></td>
            </tr>
            <tr>
                <td height="36">预浏图：</td>
                <td><input id="txtPreviewImageUrl" name="txtPreviewImageUrl" type="text" class="input_16" style="width:96%; " maxlength="500" /></td>
            </tr>
            <tr>
                <td height="36">默认的背景图：</td>
                <td><input id="txtBackgroundImageUrl" name="txtBackgroundImageUrl" type="text" class="input_16" style="width:96%; " maxlength="500" /></td>
            </tr>
            <tr>
                <td height="36">说明：</td>
                <td>
                    <textarea rows="5" class="input_16" id="txtDescription" name="txtDescription" style="width:96%; overflow:scroll;margin-bottom:10px;"></textarea>
                </td>
            </tr>
            <tr>
                <td height="36">模版内容：</td>
                <td>
                    <textarea rows="15" class="input_16" id="txtTemplate" name="txtTemplate" style="width:96%; overflow:scroll;"></textarea>
                </td>
            </tr>
            <tr>
                <td height="36">预览HTML：</td>
                <td>
                    <textarea rows="15" class="input_16" id="txtPreviewHtml" name="txtPreviewHtml" style="width:96%; overflow:scroll;"></textarea>
                </td>
            </tr>
        </table>
    </form>

</div>

<div style=" background-color:#ccc; position:absolute; bottom:55px; left:20px;right:20px; height:1px;">

</div>

<div style="position:absolute; bottom:15px; left:20px;right:20px;">
    <div style="float:left;">
        <input name="btnRemove" type="button" class="btn_red" id="btnRemove" value="删 除" style="display:none" onclick="removeData()" />
    </div>

    <div style="float:right">
        <input name="btnSave" type="button" class="btn_blue" id="btnSave" value="保 存" onclick="save()" />
        <input name="btnCancel" type="button" class="btn_blue" id="btnCancel" value="取 消" onclick="cancel()" />
    </div>
    <div style="clear:both">
    </div>
</div>


